<?php


namespace App\Admin\Components;

use App\Models\Owner;
use Carbon\Carbon;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Metrics\Card;

class Room extends Card
{
    protected $labels = ['租金', '押金', '租客姓名', '离租期结束'];

    protected $room;
    protected $owner;

    public function __construct($room, $title = null, $icon = null)
    {
        $this->room = $room;
        $this->owner = Owner::query()->with('user')->where('ownerable_type', \App\Models\Room::class)
            ->where('ownerable_id', $this->room->id)
            ->first();
        parent::__construct($title, $icon);
    }

    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();

        $color = Admin::color();
        $colors = [$color->primary(), $color->alpha('blue2', 0.5)];
        // 判断该房间是否有客户入住
        $this->header($this->room->name);
        $this->chartLabels($this->labels);
        // 设置图表颜色
        $this->chartColors($colors);
    }

    public function header($title)
    {
        // 判断是否已经出租
        if ($this->room->is_rent) {
            $url = route("admin.running-water.history-bill", $this->owner->id);
            $button = "<span class='btn btn-sm bg-light shadow-0 p-0'><a href='{$url}'>租客历史流水</a></span>";
            $title = "$title <span style='color: green; font-size: 12px;'>(已入住)</span>";
        } else {
            $url = route('admin.owner.create')."?room_id=".$this->room->id;
            $button = "<span class='btn btn-sm bg-light shadow-0 p-0'><a href='{$url}'>业主入住</a></span>";
            $title = "$title <span style='color: red; font-size: 12px;'>(等待出租)</span>";
        }

        return parent::header(
            <<< HTML
            <div>
                <h4 class="card-title mb-1">{$title}</h4>
                <div class="metric-header"></div>
            </div>
            {$button}
HTML
        );
    }


    /**
     * 渲染模板
     *
     * @return string
     */
    public function render()
    {
        $this->fill();

        return parent::render();
    }

    /**
     * 写入数据.
     *
     * @return void
     */
    public function fill()
    {
        if ($this->room->is_rent) {
            // 每期租金
            $str_rent_price = $this->owner->cycle_price."元/".Owner::$cycleUnitMap[$this->owner->cycle_unit];
            // 押金
            $str_deposit = $this->owner->deposit."元";
            $str_name = $this->owner->user->name;
//            $str_ended_at = Carbon::parse($this->owner->ended_at)->timestamp;
            $str_ended_at = (Carbon::parse($this->owner->ended_at)->timestamp - Carbon::now()->timestamp) / 60 / 60 / 24;
            $str_ended_at = ceil($str_ended_at) . "天";
            $this->withContent($str_rent_price, $str_deposit, $str_name, $str_ended_at);
        } else {

        }
    }

    /**
     * 设置卡片头部内容.
     *
     * @param mixed $desktop
     * @param mixed $mobile
     *
     * @return $this
     */
    protected function withContent($str_rent_price, $str_deposit, $str_name, $str_ended_at)
    {
        $blue = Admin::color()->alpha('blue2', 0.5);
        $style = 'margin-bottom: 8px; display: flex; justify-content: space-between';
        $labelWidth = 120;
        $marginLeft = 20;
        $userShow = route("admin.user.show", ['user' => $this->owner->user->id]);

        return $this->content(
            <<<HTML
<div class="d-flex pl-1 pr-1 pt-1" style="{$style}">
    <div style="display: flex; justify-content: space-between; width: 100%;">
        <div style="width: {$labelWidth}px">
            <i class="fa fa-circle text-primary"></i> {$this->labels[0]}
        </div>
        <div>{$str_rent_price}</div>
    </div>
    <div style="display: flex; justify-content: space-between; width: 100%;">
        <div style="width: {$labelWidth}px; margin-left: {$marginLeft}px">
            <i class="fa fa-circle text-primary"></i> {$this->labels[2]}
        </div>
        <div><a href="{$userShow}">{$str_name}</a></div>
    </div>
</div>
<div class="d-flex pl-1 pr-1" style="{$style}">
    <div style="display: flex; justify-content: space-between; width: 100%;">
        <div style="width: {$labelWidth}px">
            <i class="fa fa-circle" style="color: $blue"></i> {$this->labels[1]}
        </div>
        <div>{$str_deposit}</div>
    </div>
    <div style="display: flex; justify-content: space-between; width: 100%;">
        <div style="width: {$labelWidth}px; margin-left: {$marginLeft}px">
            <i class="fa fa-circle text-primary"></i> {$this->labels[3]}
        </div>
        <div>{$str_ended_at}</div>
    </div>
</div>
HTML
        );
    }
}